﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using HalconDotNet;
using System.Threading;
using ProducerConsumeClass;

namespace PLCDEMO
{
    public partial class Form1 : Form
    {
        public  HObject DstImage,Threshold_Region;
        private static Form1 fr1 = null;
        //存放图像的数组
        private HObject[] ImageArrary = new HObject[8];
        public HTuple hv_AcqHandle = null;
     
        private static HTuple WindowID;
        private bool video_flag = false;

        //private Thread dispig;
        //private HTuple image_width, image_height;
        //private int threshold_l, threshold_h;

        private bool m_btnState =false;
        public static bool video_enable { get; set; }
        public HObject Image;

        ThreadTest2 tt2;
        Thread tt3; //生产消费模式的 假线程

        //单例模式
        public static Form1 CreateInstrance()
        {
            if (fr1 == null || fr1.IsDisposed )
            {
                fr1 = new Form1();
            }

            return fr1;
        }

        public static HTuple cameraCatchEnable
        {
           get { return WindowID; }
        }

        public Form1()
        {
            InitializeComponent();
            Image = new HObject();
            CreateHalconWindow();
            video_enable = false;
            //LoadImage();
        }

        #region 图像窗体初始化
        public void CreateHalconWindow()
        {
            HTuple FatherWindow = hw1.HalconWindow;
            //设置窗口的背景颜色
            HOperatorSet.SetWindowAttr("background_color", "black");

            HOperatorSet.OpenWindow(0, 0, hw1.Width, hw1.Height, FatherWindow, "visible", "", out WindowID);


        }
        #endregion

        public void LoadImage()
        {
            //读取一张图像
            HOperatorSet.ReadImage(out Image, "1.bmp");
            HTuple width = null, height = null;

            //获取图像大小
            HOperatorSet.GetImageSize(Image, out width, out height);

            //设置对象显示的颜色
            //HOperatorSet.SetColor(WindowID, "yellow");

            //通过改变图像的缩放来适应图像在窗口的正常显示
            HOperatorSet.SetPart(WindowID, 0, 0, height, width);

            //在窗口上显示图像
            HOperatorSet.DispObj(Image, WindowID);
        }


        private void button_video_Click(object sender, EventArgs e)
        {
            if(video_flag == false)
            {
                video_enable = true;
                tt2 = new ThreadTest2(WindowID,Form2.WindowID);
                tt2.setStopEnable = false;

                tt3 = new Thread(tt2.Test);

                tt3.Start();
                button_video.Text = "关闭相机";
                video_flag = true;
            }
            else
            {
                video_enable = false;
                if (tt2 != null)
                {
                    tt2.setStopEnable = true;
                    tt3.Abort();
                    tt2 = null;                                      
                }          
                video_flag = false;
                button_video.Text = "打开相机";
            }          

        }

        private void radioButton1_Click(object sender, EventArgs e)
        {
            if (m_btnState == false)
            {

                this.radioButton1.Checked = true;
                this.m_btnState = true;
            }
            else
            {
                this.radioButton1.Checked = false;
                this.m_btnState = false;
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            //HOperatorSet.IF
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }


    }




}
